#pragma once
#include <iostream>

#include <set>
#include <map>
#include <sys/timeb.h>
#include <queue>
#include <functional>
#include <csignal>
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/core/cvstd.hpp"
#include "opencv2/videoio.hpp"
#include "ShareQueue.h"
#include "yolov7.h"
#include "AppShareMemory.h"
#include "Thread.h"
#include <jmorecfg.h>

using namespace std;
using namespace cv;


class CameraReader:public Thread
{
public:
	CameraReader(string channel,string url, string mask_file_name, bool calibrate = false,
		bool shouldResize = false, vector<int> resizeDimension = { 0,0 }, 
		bool shouldCrop = false, vector<int> cropRectangle = { 0,0,0,0 },
		int memorySize=5, boolean netCamera = false);

	~CameraReader();

	void shouldResize(bool shouldResize);

	void shouldCalibrate(bool shouldCalibrate);

	void setFrameQueue(ShareQueue<FrameInfo>* frameQueue);

	void setObjectDetectionQueue(ShareQueue<std::vector<Detection>>* objectDetectionQueue);

	void setExtractFrequency(int frameCount);

	void switchObjectDetection(bool on);

	bool initialize();
protected:
	virtual void process() override;

private:

	string channel;

	string url;

	string mask_file;

	bool netCamera = false;
	
	long ip_number;

	bool needCalibrate = false;

	bool needResize = false;

	bool needCrop = false;

	int memorySize = 5;

	int blockSize = 0;
	int dataSize = 0;
	int itemLength = 0;
	int frameIndex = 0;
	bool needObjectDetection = false;

	AppShareMemory *shareMemory =  nullptr;
	unsigned char* image_array_ptr;

	ShareQueue<std::vector<Detection>>* objectDetectionQueue;
	ShareQueue<FrameInfo>* frameQueue;
	int frameFrequency;
	cv::Mat frame;
	cv::Mat source_frame;
	cv::Mat mask_frame;
	cv::Mat alpha;
	cv::VideoCapture *stream;
	cv::Size resizeDimension;
	cv::Size videoSize;
	cv::Rect cropRect;
	cv::Mat createAlpha(Size size,unsigned char value = 255);
	long get_ip_number(const string& ip_address);	
};

